package com.mystudy.management.usermanagement.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mystudy.management.usermanagement.dao.pojo.Visitor;
import com.mystudy.management.usermanagement.laytablemessageconvert.LayuiTableMessageConvert;
import com.mystudy.management.usermanagement.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;



import java.util.Collection;


@RestController
@RequestMapping("/admin/index")//使用restful风格进行匹配
public class IndexController {

    @Autowired
    private IndexService indexService;//注入服务

    @Autowired
    private LayuiTableMessageConvert convert;//注入打包工具




    /*
       --------------------------------------------- 授权管理控制器----------------------------------------------------------------
     */



    //查询所有授权用户,返回数据给前端表格

    @GetMapping("/authorize/visitors")
    public Object selectAllVisitors() {
        Collection<Visitor> result = indexService.selectAllVisitors();//调用查询服务
        return convert.indexMessagePacking(result);
    }

    //根据指定的id或者用户名查询用户

    @GetMapping("/authorize/visitors/{choice}/{message}")
    public Object selectVisitorsByCondition(@PathVariable("choice")String choice,
                                            @PathVariable("message") String message){
        System.out.println("前端："+choice+","+message);
        Visitor visitor = new Visitor();
        if (choice.equals("byId")){
            Integer id = Integer.valueOf(message);
            System.out.println("准备赋值:"+id);
            visitor.setUserId(id);
            visitor.setUserName(null);
            System.out.println("我的赋值1："+visitor);
        }else{
            visitor.setUserId(null);
            visitor.setUserName(message);
            System.out.println("我的赋值2："+visitor);
        }
        System.out.println("控制层想向务层传递的对象:"+visitor);
        Collection<Visitor> result = indexService.selectVisitorByExistCondition(visitor);
        return convert.indexMessagePacking(result);
    }


    //数据删除事件控制器

    @DeleteMapping("/authorize/visitors/{id}")
    public String deleteVisitorController(@PathVariable("id") Integer id) {
        System.out.println("删除控制器从前端获取的参数:" + id);
        boolean result = this.indexService.deleteVisitorById(id);//获取删除结果
        if (result) {
            return "删除成功！";
        }
        return "删除失败，请重试";


    }


    //数据修改事件控制器

    @PostMapping("/authorize/visitors")
    public String modifyVisitorController(@RequestParam("userId") Integer id,
                                          @RequestParam("userPassword") String password,
                                          @RequestParam("userName") String username){

        System.out.println("控制层："+id);
        Visitor visitor = new Visitor();
        visitor.setUserName(username);
        visitor.setUserId(id);
        visitor.setPassword(password);
        if(indexService.updateVisiterMessage(visitor)) { //调用业务层方法
            return "修改成功";
        }else {
            return "修改失败，用户不存在，请尝试刷新一下";
        }


    }

    //数据添加事件控制器

    @PutMapping("/authorize/visitors")
    public boolean addVisitorController(@RequestBody JSONObject visitorJsonMsg){
        System.out.println("[控制层] 接收到的参数"+visitorJsonMsg);
        JSONObject userInfo = visitorJsonMsg.getJSONObject("userInfo");
        userInfo.put("userId",null);
        Visitor visitor = JSON.toJavaObject(userInfo,Visitor.class);
        System.out.println("[控制层] 构造的实体类"+visitor);
        return indexService.addVisitor(visitor);

    }







}
